/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.AreaComparator;
import com.minierp.dao.AreaDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Area;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AreaMACtr extends MultiActionController {
	private AreaDao areaDao;
	private String listView;

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "BASE_AREA")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.listView);
			String parentId = request.getParameter("parent_id");
			if (parentId != null && !parentId.equals("")) {
				Area p = (Area) this.areaDao.get(Area.class,
						Long.parseLong(parentId));
				if (p != null) {
					mv.addObject("retId", String.valueOf(p.getParentId()));
				}
			} else {
				parentId = "0";
			}

			mv.addObject("list", this.sort(this.areaDao.getList(
					Long.parseLong(parentId), true)));
			mv.addObject("areaList", this.areaDao.getList(true));
			mv.addObject("parentId", parentId);
			return mv;
		}
	}

	private Object[] sort(List list) {
		Object[] objs = list.toArray();
		Arrays.sort(objs, new AreaComparator());
		return objs;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String returnUrl = request.getParameter("from_url");
		ModelAndView mv = new ModelAndView("redirect:" + returnUrl);
		long id = Long.parseLong(request.getParameter("id"));
		this.areaDao.unenabledTree(id);
		EncCommonHelper.setAlertMessage(request, "删除成功！");
		return mv;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public AreaDao getAreaDao() {
		return this.areaDao;
	}

	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}
}